from datetime import datetime

from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime, JSON, Text
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    """用户模型"""
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    hashed_password = Column(String)
    nickname = Column(String)
    avatar = Column(String)
    roles = Column(String)
    permissions = Column(String)
    is_active = Column(Boolean, default=True)

    def to_dict(self):
        return {
            "id": self.id,
            "username": self.username,
            "nickname": self.nickname,
            "avatar": self.avatar,
            "roles": self.roles,
            "permissions": self.permissions,
            "is_active": self.is_active
        }


class Device(Base):
    __tablename__ = "devices"

    id = Column(Integer, primary_key=True, index=True)
    dev_id = Column(String, unique=True, index=True)
    dev_name = Column(String)
    cpu_ser = Column(String)
    emmc_ser = Column(String)
    mac_add = Column(String)
    activated_time = Column(String)
    cpu_usage = Column(Integer, default=0)
    mem_usage = Column(Integer, default=0)
    # emmc_usage = Column(Integer, default=0)
    # ip_address = Column(String)
    city = Column(String)
    firmware_version = Column(String)
    app_version = Column(String)
    online_status = Column(String)
    brightness = Column(Integer, nullable=True)
    volume = Column(Integer, nullable=True)
    last_active = Column(Float)

    def to_dict(self):
        """将设备对象转换为字典"""
        return {
            "id": self.id,
            "dev_id": self.dev_id,
            "dev_name": self.dev_name,
            "cpu_ser": self.cpu_ser,
            "emmc_ser": self.emmc_ser,
            "mac_add": self.mac_add,
            "activated_time": self.activated_time,
            "cpu_usage": self.cpu_usage,
            "mem_usage": self.mem_usage,
            # "emmc_usage": self.emmc_usage,
            # "ip_address": self.ip_address,
            "city": self.city,
            "firmware_version": self.firmware_version,
            "app_version": self.app_version,
            "online_status": self.online_status,
            "brightness": self.brightness,
            "volume": self.volume,
            "last_active": self.last_active
        }


class DeviceRegistrationLog(Base):
    __tablename__ = "device_registration_logs"

    id = Column(Integer, primary_key=True, index=True)
    registration_time = Column(DateTime, default=datetime.utcnow, nullable=False)
    success = Column(Boolean, nullable=False)
    request_data = Column(JSON, nullable=False)  # 存储原始请求数据
    response_data = Column(JSON, nullable=True)  # 存储响应数据
    error_message = Column(String(500), nullable=True)
    dev_id = Column(String(64), index=True, nullable=True)  # 如果成功注册的设备ID
    ip_address = Column(String(50), nullable=True)  # 请求来源IP
    user_agent = Column(String(255), nullable=True)  # 用户代理信息


class DeviceDeletionLog(Base):
    __tablename__ = "device_deletion_logs"

    id = Column(Integer, primary_key=True, index=True)
    deletion_time = Column(DateTime, nullable=False)
    success = Column(Boolean, nullable=False)
    dev_id = Column(String(255), nullable=False)
    device_info = Column(Text)  # 存储设备信息的JSON字符串
    ip_address = Column(String(50))
    user_agent = Column(Text)
    error_message = Column(Text)